package cn.cyh.org.controller;

import cn.cyh.basic.exception.BusinessException;
import cn.cyh.basic.uitls.AJaxResult;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.org.domain.Shop;
import cn.cyh.org.query.EmployeeQuery;
import cn.cyh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    // 新增和修改 put
    @PutMapping
    public AJaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            System.out.println("");
            if (shop.getId()==null){ // 如果没有则为添加
                shopService.save(shop);
            } else{ // 否则是修改
                shopService.update(shop);
            }
            return AJaxResult.myself();

        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }
    // 删除 delete
    @DeleteMapping("/{id}")
    public AJaxResult remove(@PathVariable("id") Long id){
        try {
            System.out.println("");
            shopService.remove(id);
            return AJaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }

    // 查询 get
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    // 查询所有部门
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.loadAll();
    }

    // 分页查询
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody EmployeeQuery query){
        PageList<Shop> employeePageList = shopService.queryPage(query);
        return employeePageList;
    }
    // 批量删除
    @PatchMapping
    public AJaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            System.out.println("");
            shopService.patchDelete(ids);
            return AJaxResult.myself();
        } catch (Exception e){
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }

    @PostMapping("/settlement")
    public AJaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AJaxResult.myself();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("操作失败");
        }
    }

}
